package com.applancer.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.applancer.adapter.BookmarksAdapter;
import com.applancer.dataprovider.DataType.Type;
import com.applancer.dataprovider.DatabaseHandler;
import com.applancer.delegate.NewFeedDelegate;
import com.applancer.model.FeedModel;
import com.applancer.model.GeneralModel;
import com.applancer.nativecmsflip.FeedDetailActivity;
import com.applancer.nativecmsflip.R;
import com.applancer.parser.GeneralParser;
import com.applancer.provider.StringUtils;

public class FeedBookmarksFragment extends FeedGeneralFragment {
	protected NewFeedDelegate delegate;
	protected ListView listView;
	protected BookmarksAdapter adapter;
	protected List<GeneralModel> listData;
	protected Boolean loadingMore = false;
	protected String title;
	protected ProgressBar indicator_loading_feed;

	public FeedBookmarksFragment(String i, String title, NewFeedDelegate del) {

		delegate = del;
		id = i;
		this.title = title;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		contentView = inflater.inflate(R.layout.feed_bookmarks_fragment, null);
		return contentView;

	}

	@Override
	protected void initial() {
		super.initial();

		listData = new ArrayList<GeneralModel>();
		adapter = new BookmarksAdapter(contentView.getContext(), listData);
		listView = (ListView) contentView.findViewById(R.id.list_feed);
		indicator_loading_feed = (ProgressBar) contentView
				.findViewById(R.id.indicator_loading_feed);

		parser = GeneralParser.createInstance(Type.Post);

	}

	@Override
	protected void execution() {
		super.execution();

		listView.setOnItemClickListener(itemClick);
		listView.setVerticalScrollBarEnabled(false);

		listView.setAdapter(adapter);

		fitListView();

	}

	@Override
	public void onResume() {

		super.onResume();
		indicator_loading_feed.setVisibility(View.VISIBLE);
		(new Handler()).postDelayed(new Runnable() {

			@Override
			public void run() {
				listData.clear();
				getBookmarks();
				indicator_loading_feed.setVisibility(View.GONE);
			}
		}, 500);
	}

	private void getBookmarks() {

		DatabaseHandler sqlite = new DatabaseHandler(getActivity());

		List<FeedModel> newData = sqlite.getAllModel();

		if (newData.size() == 0)
			return;

		listData.addAll(newData);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);
		fitListView();
		adapter.notifyDataSetChanged();
	}

	private void fitListView() {
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		boolean hasMenuKey = ViewConfiguration.get(contentView.getContext())
				.hasPermanentMenuKey();
		if (currentapiVersion >= 19) {
			Resources r = getResources();
			int px = (int) r.getDimension(R.dimen.padding_materio);
			// (int) TypedValue.applyDimension(
			// TypedValue.COMPLEX_UNIT_DIP,
			// getResources().getInteger(R.integer.padding_materio),
			// r.getDisplayMetrics());

			listView.setPadding(
					px,
					StringUtils.getStatusBarHeight(getActivity())
							+ StringUtils.getActionbarHeight(getActivity())
							+ px,
					px,
					px
							+ (!hasMenuKey ? StringUtils
									.getNavigationBarHeigh(getActivity()) : 0));
		}
	}

	OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			// GeneralModel model = (GeneralModel) parent.getAdapter().getItem(
			// position);

			Intent intent = new Intent(contentView.getContext(),
					FeedDetailActivity.class);

			ArrayList<FeedModel> lmodeldata = new ArrayList<FeedModel>();
			for (int i = 0; i < listData.size(); i++) {
				lmodeldata.add((FeedModel) listData.get(i));
			}

			intent.putExtra("lmodel", lmodeldata);
			intent.putExtra("index", position);
			intent.putExtra("title", title);

			contentView.getContext().startActivity(intent);
			if (getActivity() != null)
				getActivity().overridePendingTransition(R.anim.slide_left_in,
						R.anim.slide_left_out);
		}
	};

}